package com.fourfaith.iot.beidou.controller;

import com.fourfaith.common.pojo.ResultDto;
import com.fourfaith.common.utils.AlgorithmUtils;
import com.fourfaith.iot.beidou.pojo.bean.FkxxBean;
import com.fourfaith.iot.beidou.pojo.dto.BeiDouSendDto;
import com.fourfaith.iot.beidou.utils.MessageParseUtil;
import com.fourfaith.iot.beidou.enums.FkxxEnum;
import com.fourfaith.iot.beidou.manager.SerialPortManager;
import com.fourfaith.iot.device.cloud.access.dubbo.pojo.param.BeiDouParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.*;

/**
 * @Name MessageController
 * @Description 消息控制器
 * @Author shiyunan
 * @Create 2021/11/11 17:23
 */
@RestController
@RequestMapping(value = "/iot-beidou/message", produces = "application/json;charset=UTF-8")
@Slf4j
public class MessageController {

    @Autowired
    private SerialPortManager serialPortManager;

    @PostMapping("/send")
    public ResultDto<BeiDouSendDto> send(@RequestBody BeiDouParam beiDouParam) {
        log.info("北斗信息下发：{}", beiDouParam);
        FkxxBean fkxxBean = null;
        try {
            fkxxBean = serialPortManager.send(beiDouParam);
        } catch (Exception e) {
            log.error("北斗信息发送失败", e);
        }
        BeiDouSendDto beiDouSendDto = new BeiDouSendDto();
        beiDouSendDto.setSendResult(fkxxBean.getSuccess());
        beiDouSendDto.setErrorMessage(fkxxBean.getErrorMessage());
        return ResultDto.returnSuccessData(beiDouSendDto);
    }

    @GetMapping("/test")
    public ResultDto<Boolean> test() {
        String payloadString = "24464B58580010065F57005458535139";
        byte[] ary = AlgorithmUtils.HexString2Bytes(payloadString);
        BeiDouParam beiDouParam = MessageParseUtil.parse(ary);
        return ResultDto.returnSuccessData(true);
    }
}
